import DAL from './dal.js';
import util from './util.js'
/**
 * 跟B01单位表相关的方法
 * todu:增加了B0194字段，还需要做逻辑上的改动 B0194代表是否是法人机构
 */
class B01DAL {
	constructor() {
		this.init();
	};

	init() {
		this.db = new DAL();
		this.util = new util();
	};

	//在js中 数组和json应该是一样的，数组可以当作key是0 1 2 的json
	toCanRead(originalJsonInfoArray) {
		//console.log(originalJsonInfoArray)
		let canReadArray = new Array();
		for (let key in originalJsonInfoArray) {

			let originalJson = originalJsonInfoArray[key];//这个才是真正的json

			let temp = {};
			for (let item in originalJson) {

				if(originalJson[item]==null||originalJson[item]=='null'){
                    originalJson[item]='';
                }

				if (this.util.B01Dictionary[item]) {

					temp[this.util.B01Dictionary[item]] = originalJson[item]
				}

			}
			canReadArray.push(temp)
		}


		return canReadArray;
	}

	/**
	 * @param {Object} orgName
	 * 2通过单位名称、单位名称简写 单位名称拼音 单位名称拼音简写 单位名称简写拼音  单位名称简写拼音的简写 六个查询单位 查到机构ID
	 * 返回单位内部ID数组 B0111
	 */
	async SelectOrgIDByOrgName(orgName) {
		return this.toCanRead(await this.db.SelectDataFieldArrayFromDB('B01', 'DISTINCT B0111', `WHERE  B0194 is not 3 AND 
											( B01.b0101 LIKE \'%${orgName}%\' OR
											B01.B0104 LIKE \'%${orgName}%\' ) `));
	}
	/**
	 * 返回指定ID的机构的全部下属机构的信息
	 * @param {Object} orgID 获取下级单位的机构ID
	 * @return {Array}
	 */
	async SelectBranchOrgInfoByOrgID(orgID) {
		return this.toCanRead(await this.db.SelectDataFieldArrayFromDB('B01', 'b0101,B0104,B0111,B0121,SORTID', `WHERE B0194 is not 3 AND B0121 LIKE \'${orgID}%\'  ORDER BY SORTID`));
	}

	/**
	 * 通过全称找到机构信息
	 * @param {Object} orgName
	 */
	async SelectExactOrgInfoByOrgName(orgName) {
		return this.toCanRead(await this.db.SelectDataFieldArrayFromDB('B01', 'b0101,B0104,B0111,B0121,SORTID', `WHERE  B0194 is not 3 AND 
											B01.b0101 = \'${orgName}\'  ORDER BY SORTID`));
	}

	/**
	 * 返回指定ID的机构的直接下属机构，不包括下属的分支  这个要测试
	 * @param {Object} orgID
	 */
	async SelectDirectBranchOrgInfoByOrgID(orgID) {
		return this.toCanRead(await this.db.SelectDataFieldArrayFromDB('B01', 'b0101,B0104,B0111,B0121,SORTID ', `WHERE B0194 is not 2 AND  B0121 = \'${orgID}\'  ORDER BY SORTID`));
	}
	/**
	 * 返回组织机构树
	 */
	async SelectOrgTree() {
		let orgArray = new Array();

		let tempInfo = await this.SelectExactOrgInfoByOrgName('安阳市人民政府')

		if (tempInfo.length > 0) {
			let topOrgId = tempInfo[0].SuperiorOrgID;//根是不显示的,得到根的机构编号，就是政府的上一级，这样能查到第一级要显示的机构

			let shijiArray = await this.SelectDirectBranchOrgInfoByOrgID(topOrgId);//得到需要显示的第一级的单位 是政府一级

			for (let i = 0; i < shijiArray.length; i++) {
				//这是第一层机构
				let tempOrg = {
					OrgID: shijiArray[i].OrgID,//机构内部ID
					OrgFullName: shijiArray[i].OrgFullName,//机构全程
					OrgShortName: shijiArray[i].OrgShortName, //机构简称
					SORTID: shijiArray[i].SORTID,//同级别内的排序
					BranchOrg: []
				}
				//console.log(tempOrg)
				if ((tempOrg.OrgFullName != '安阳市人民代表大会常务委员会') && (tempOrg.OrgFullName != '政协安阳市委员会') && (tempOrg.OrgFullName != '安阳市中级人民法院') && (tempOrg.OrgFullName != '安阳市人民检察院')&& (tempOrg.OrgFullName != '安阳市纪律检查委员会(市监察委)')) {
					//增加这个机构的子机构
					let branchOrgArray = await this.SelectDirectBranchOrgInfoByOrgID(tempOrg.OrgID);//查询局一级，但这只是临时这样做

					for (let j = 0; j < branchOrgArray.length; j++) {
						let tempBranchOrg = {
							OrgID: branchOrgArray[j].OrgID,//机构内部ID
							OrgFullName: branchOrgArray[j].OrgFullName,//机构全程
							OrgShortName: branchOrgArray[j].OrgShortName, //机构简称
							SORTID: branchOrgArray[j].SORTID,//同级别内的排序
							BranchOrg: []
						}
						tempOrg.BranchOrg.push(tempBranchOrg);
					}
				}

				orgArray.push(tempOrg);
			}
		}
		else {
			return [];
		}
		return orgArray;
	}


	//得到一个机构以及其下属机构的信息，按机构级别（ID长度）和同级别顺序排序
	async SelectOrgInfoByOrder(orgID){
		return this.toCanRead(await this.db.SelectDataFieldArrayFromDB('B01', 'b0101,B0104,B0111,B0121,SORTID ', `WHERE B0194 is not 2 AND  B0111 like \'${orgID}%\'  ORDER BY length(B0111),length(SORTID),SORTID `));
	}


	async getOrgTreeOrderInfo(orgID) {
		let sqlString = `with recursive digui as 
(
 SELECT * from B01 where B0111 = '001.001.001.001.00U'
 UNION ALL
 SELECT B01.* from digui JOIN B01 ON digui.B0121 = B01.B0111 
)
select digui.B0104,digui.B0114,digui.B0111,digui.B0121,digui.B0194,digui.SORTID from digui ORDER BY B0111 ASC  `;
		return await this.db.SelectDataFieldArrayFromDBByCmd(sqlString)
	}

}

export default B01DAL;